﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using VISAInstrument.Ports;
using VM.Shared.TestAttribute;

namespace 信号发生器SG1501B
{
    public class 信号输出
    {
        private static PortOperatorBase _portOperatorBase;
        [ExposedMethodAttribute("")]
        public bool FM(string 端口名称, out string 错误信息, out string 返回值,double 调制 = 22.5, double 频率 = 1000.0, double 幅值 = 30.0,string 返回值格式 = "调制：{调制}KZ，频率：{频率}MZ，幅值：{幅值}DU")
        {
            错误信息 = "";
            返回值 = "";
            try
            {
                返回值 = 返回值格式.Replace("{调制}", 调制.ToString())
                                   .Replace("{频率}", 频率.ToString())
                                   .Replace("{幅值}", 幅值.ToString());
                for (int i = 0; i < 2; i++)
                {
                    _portOperatorBase = new GpibPortOperator(端口名称);
                    _portOperatorBase.Open();
                    _portOperatorBase.Write($"FM {调制}KZ");//FM 106.1KZ
                    Thread.Sleep(5);
                    _portOperatorBase.Write($"FR {频率} MZ");
                    Thread.Sleep(5);
                    _portOperatorBase.Write($"LE {幅值} DU");
                    Thread.Sleep(5);
                    _portOperatorBase.Write("MR2");
                    Thread.Sleep(5);
                    _portOperatorBase.Write("FM ON");
                    Thread.Sleep(5);
                    _portOperatorBase.Close();
                }
            }
            catch (Exception ex)
            {
                错误信息 = ex.Message;
                return false;
            }
            return true;
        }

        [ExposedMethodAttribute("")]
        public bool AM(string 端口名称, out string 错误信息, double 调制 = 30, int 频率 = 666, int 幅值 = 30)
        {
            错误信息 = "";
            try
            {

                for (int i = 0; i < 2; i++)
                {
                    _portOperatorBase = new GpibPortOperator(端口名称);
                    _portOperatorBase.Open();
                    _portOperatorBase.Write($"AM {调制} PC");
                    _portOperatorBase.Write($"FR {频率} KZ");
                    _portOperatorBase.Write($"LE {幅值} DU");
                    _portOperatorBase.Write("AM ON");
                    _portOperatorBase.Close();
                }
            }
            catch (Exception ex)
            {
                错误信息 = ex.Message;
                return false;
            }
            return true;
        }

        [ExposedMethodAttribute("")]
        public bool 关闭调制(string 端口名称, out string 错误信息)
        {
            错误信息 = "";
            try
            {
                for (int i = 0; i < 2; i++)
                {
                    _portOperatorBase = new GpibPortOperator(端口名称);
                    _portOperatorBase.Open();
                    _portOperatorBase.Write("M0");
                    _portOperatorBase.Close();
                }
            }
            catch (Exception ex)
            {
                错误信息 = ex.Message;
                return false;
            }

            return true;
        }
    }
}
